package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.DeleteGoodsOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.GoodsDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.GoodsCommand;
import com.cskaoyan.wordmemorize.request.GoodsPageRequest;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.service.GoodsService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/goods")
public class AdminGoodsController {

    @Resource
    GoodsService goodsService;

    // 获取商品分页列表（支持无条件和按名称查询）
    @GetMapping("/list")
    public Result<PageDTO<GoodsDTO>> list(GoodsPageRequest request) {

        PageDTO<GoodsDTO> page = goodsService.getGoodsPage(request);

        return Result.ok(page);
    }

    // 添加商品
    @PostMapping("")
    public Result<Void> add(@RequestBody GoodsCommand command) {

        goodsService.addGoods(command);

        return Result.ok();
    }

    // 修改商品
    @PutMapping("")
    public Result<Void> update(@RequestBody GoodsCommand command) {

        goodsService.updateGoods(command);

        return Result.ok();
    }

    // 修改商品上下架状态
    @PutMapping("/status/{goodsId}")
    public Result<Void> updateStatus(@PathVariable Long goodsId, @RequestParam Integer status) {

        goodsService.updateGoodsStatus(goodsId, status);

        return Result.ok();
    }

    // 删除商品
    @DeleteMapping("/{goodsId}")
    public Result<DeleteGoodsOperateDTO> delete(@PathVariable Long goodsId) {

        DeleteGoodsOperateDTO dto = goodsService.deleteGoods(goodsId);

        return Result.ok(dto);
    }

    // 批量删除选中商品
    @DeleteMapping("/selected")
    public Result<List<DeleteGoodsOperateDTO>> deleteSelected(@RequestBody BatchDeleteCommand command) {

        List<DeleteGoodsOperateDTO> dtos = goodsService.deleteGoodsSelected(command);

        return Result.ok(dtos);
    }
}
